﻿
using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace YDT_ProductService.Registrys
{
    /// <summary>
    /// 1、分布式服务注册模块-封装
    /// </summary>
    public static class ConsulDistributedServiceRegistryWebApplicationBuilderExtenions
    {
        /// <summary>
        /// consul扩展方法
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        public static WebApplicationBuilder AddDistributedServiceRegistry(this WebApplicationBuilder builder)
        {
            // 1、获取配置
            var Configuration = builder.Configuration;
            // 1、注册心跳检测
            builder.Services.AddHealthChecks();
            // 2、注册ConsulServiceRegistry
            builder.Services.AddSingleton<ConsulServiceRegistry>();
            // 3、注册ServiceRegistryHostService
            builder.Services.AddHostedService<ServiceRegistryHostService>();
            // 4、注册ServiceRegistryOptions (获取值，然后注册 到IOC容器)
            builder.Services.Configure<ServiceRegistryOptions>(Configuration.GetSection("DistributedServiceRegistry"));

            return builder;
        }
    }
}
